package com.hmyd.hmyd_android.view.ad;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TableLayout.LayoutParams;

/**
 * 界面布局
 * 
 **/
public class MyLayout {

	public static int getWidth(Context context) {

		DisplayMetrics displayMetrics = new DisplayMetrics();
		WindowManager wm = (WindowManager) context.getApplicationContext()
				.getSystemService("window");
		wm.getDefaultDisplay().getMetrics(displayMetrics);
		int width = displayMetrics.widthPixels;
		return width;
	}

	public static android.widget.LinearLayout.LayoutParams getHideLinearLayout() {

		android.widget.LinearLayout.LayoutParams wrap = new android.widget.LinearLayout.LayoutParams(
				0, 0);

		return wrap;
	}

	public static android.widget.LinearLayout.LayoutParams getWidthHideLinearLayout() {

		android.widget.LinearLayout.LayoutParams width_fill = new android.widget.LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, 0);

		return width_fill;

	}

	public static android.widget.LinearLayout.LayoutParams getWidthLinearLayout() {

		android.widget.LinearLayout.LayoutParams width_fill = new android.widget.LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

		return width_fill;

	}

	public static android.widget.LinearLayout.LayoutParams getHeightLinearLayout() {

		android.widget.LinearLayout.LayoutParams height_fill = new android.widget.LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);

		return height_fill;

	}

	public static android.widget.LinearLayout.LayoutParams getAllLinearLayout() {

		android.widget.LinearLayout.LayoutParams all_fill = new android.widget.LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		return all_fill;

	}

	public static android.widget.LinearLayout.LayoutParams getWrapLinearLayout() {

		android.widget.LinearLayout.LayoutParams wrap_fill = new android.widget.LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		return wrap_fill;

	}

	public static void setListViewHeight(ListView lv) {
		ListAdapter la = lv.getAdapter();
		if (null == la) {
			return;
		}
		// calculate height of all items.
		int h = 0;
		final int cnt = la.getCount();
		for (int i = 0; i < cnt; i++) {
			View item = la.getView(i, null, lv);
			item.measure(0, 0);
			h += item.getMeasuredHeight();
		}
		// reset ListView height
		ViewGroup.LayoutParams lp = lv.getLayoutParams();
		lp.height = h + (lv.getDividerHeight() * (cnt - 1));
		lv.setLayoutParams(lp);
	}

	public static int getScreenWidth(Activity mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getScreenHeight(Activity mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		mContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}
	
}
